package com.weather;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class MyJson {
   static public ArrayList<Weather>getWeather(String str) throws Exception{
      ArrayList<Weather> arrayList = new ArrayList<>();

      JSONObject json = new JSONObject(str);


      JSONArray results = json.getJSONArray("results");
      JSONObject all = (JSONObject) results.get(0);

      //城市名称
      String city = all.getJSONObject("location").getString("name");

      //每日天气
      JSONArray daily = all.getJSONArray("daily");

      for(int i=0;i<daily.length();i++){
         Weather weather =new Weather();
         weather.setCity(city);
         JSONObject day = (JSONObject) daily.get(i);
         //日期
         String date = day.getString("date");
         weather.setDate(date);
         //最低气温
         String low = day.getString("low");
         weather.setLow(low);
         //最高气温
         String high = day.getString("high");
         weather.setHigh(high);

         arrayList.add(weather);
      }
     return arrayList;
   }


}
